package com.yonyou.brigade.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.yonyou.brigade.basic_init.enums.ChannelEnum;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.basic_init.model.Developer;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.basic_init.service.IDeveloperService;
import com.yonyou.brigade.basic_init.service.IUserCenterService;
import com.yonyou.brigade.rtpnr.service.IRtpnrOrderService;
import com.yonyou.brigade.third_parties.bfjr.service.IBfjrOrderService;
import com.yonyou.brigade.third_parties.ctripBC.model.CtripBCConfig;
import com.yonyou.brigade.third_parties.ctripBC.service.ICtripBCOrderService;
import com.yonyou.brigade.third_parties.didi.model.DiDiConfig;
import com.yonyou.brigade.third_parties.didi.model.DiDiUser;
import com.yonyou.brigade.third_parties.didi.service.IDiDiOrderService;
import com.yonyou.brigade.third_parties.didi.service.IDiDiUserService;
import com.yonyou.brigade.third_parties.didi.util.DiDiUtil;
import com.yonyou.brigade.third_parties.fanscar.model.FanscarConfig;
import com.yonyou.brigade.third_parties.fanscar.service.IFanscarOrderService;
import com.yonyou.brigade.third_parties.huazhu.service.IHzOrderService;
import com.yonyou.brigade.third_parties.smectrip.service.ICtripSmeOrderService;
import com.yonyou.brigade.third_parties.tujia.service.ITujiaOrderService;
import com.yonyou.brigade.third_parties.tuniu.service.ITuniuOrderService;
import com.yonyou.brigade.third_parties.yihao.service.ICLYHOrderService;
import com.yonyou.brigade.third_parties.zhjw.model.ZhjwConfig;
import com.yonyou.brigade.third_parties.zhjw.service.IZhjwOrderService;
import com.yonyou.brigade.third_parties.zteitc.service.IZteitcOrderService;
import com.yonyou.brigade.util.mail.MailSenderUtil;
import com.yonyou.brigade.util.operateutil.DateUtils;
import com.yonyou.brigade.util.resultutil.Msg;
import com.yonyou.brigade.util.resultutil.ResultUtil;
import com.yonyou.ybzboot.starter.common.exception.BusinessException;
import com.yonyou.ybzboot.starter.common.util.StringUtil;
import com.yonyou.ybzboot.starter.mail.model.Email;
import com.yonyou.ybzboot.starter.mail.service.IMailService;
import com.yonyou.ybzboot.uc.dto.UcUserDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

@RestController
@RequestMapping("/cron")
public class GetOrderController {

    Logger log= LoggerFactory.getLogger(this.getClass());

    @Value("${brigade.version}")
    private String brigadeVersion;

    @Autowired
    @Qualifier("ctripBCOrderServiceImpl")
    private ICtripBCOrderService ctripBCOrderService;

    @Autowired
    @Qualifier("briChannelServiceImpl")
    private IBriChannelService briChannelService;

    @Autowired
    @Qualifier("developerServiceImpl")
    private IDeveloperService developerService;

    @Autowired
    private IMailService mailService;

    @Autowired
    @Qualifier("rtpnrOrderServiceImpl")
    private IRtpnrOrderService rtpnrOrderService;

    @Autowired
    @Qualifier("CLYHOrderServiceImpl")
    private ICLYHOrderService CLYHOrderService;

    @Autowired
    @Qualifier("tujiaOrderServiceImpl")
    private ITujiaOrderService TujiaOrderService;

    @Autowired
    @Qualifier("tuniuOrderServiceImpl")
    private ITuniuOrderService TuniuOrderService;

    @Autowired
    @Qualifier("zteitcOrderServiceImpl")
    private IZteitcOrderService zteitcOrderService;

    @Autowired
    @Qualifier("ctripSmeOrderServiceImpl")
    private ICtripSmeOrderService ctripSmeOrderService;

    @Autowired
    @Qualifier("diDiOrderServiceImpl")
    private IDiDiOrderService diDiOrderService;

    @Autowired
    private IFanscarOrderService fanscarOrderService;

    @Value("${brigade.cur}")
    private String curEnv;

    @Autowired
    private IUserCenterService userCenterService;

    @Autowired
    private IDiDiUserService didiUserService;

    @Autowired
    private IZhjwOrderService zhjwOrderService;

    @Autowired
    private IHzOrderService hzOrderService;

    @Autowired
    private IBfjrOrderService bfjrOrderService;

    /**
     * 手动拉取中航金网订单
     * @param param
     */
    @RequestMapping("/zhjw/getZhjwOrders")
    public void getZhjwOrders(@RequestBody JSONObject param) {
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.ZHJW.getName());
            List<String> channelConfigs = new ArrayList<>();
            for (BriChannel briChannel : briChannels) {
                String channelConfig = briChannel.getConfig();
                if (channelConfigs.contains(channelConfig)) {
                    continue;
                }
                channelConfigs.add(channelConfig);
                String tenant = briChannel.getTenant();
                ZhjwConfig zhjwConfig = (ZhjwConfig) briChannelService.getChannelConfig(tenant, ChannelEnum.ZHJW.getName(), new ZhjwConfig());
                String begin = param.getString("begin");
                String end = param.getString("end");
                try {
                    zhjwOrderService.getZhjwOrders(zhjwConfig, tenant, begin, end, briChannel.getId());
                } catch (Exception e) {
                    sendEmailToDev("中航金网", e);
                }
            }
        }
    }

    /**
     * 手动拉取泛嘉火车票订单
     * @param param
     */
    @RequestMapping("/fanscar/getFanscarTrainOrders")
    public void getFanscarTrainOrders(@RequestBody JSONObject param) {
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.FANSCAR.getName());
            List<String> channelConfigs = new ArrayList<>();
            for (BriChannel briChannel : briChannels) {
                String channelConfig = briChannel.getConfig();
                if (channelConfigs.contains(channelConfig)) {
                    continue;
                }
                channelConfigs.add(channelConfig);
                String tenant = briChannel.getTenant();
                FanscarConfig fanscarConfig = (FanscarConfig) briChannelService.getChannelConfig(tenant, ChannelEnum.FANSCAR.getName(), new FanscarConfig());
                Date begin = DateUtils.strToDateTime(param.getString("begin"));
                Date end = DateUtils.strToDateTime(param.getString("end"));
                try {
                    fanscarOrderService.getFanscarTrainOrders(fanscarConfig, tenant, begin, end, briChannel.getId());
                } catch (Exception e) {
                    sendEmailToDev("泛嘉火车票", e);
                }
            }
        }
    }

    /**
     * 手动拉取泛嘉酒店订单
     * @param param
     */
    @RequestMapping("/fanscar/getFanscarHotelOrders")
    public void getFanscarHotelOrders(@RequestBody JSONObject param) {
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.FANSCAR.getName());
            List<String> channelConfigs = new ArrayList<>();
            for (BriChannel briChannel : briChannels) {
                String channelConfig = briChannel.getConfig();
                if (channelConfigs.contains(channelConfig)) {
                    continue;
                }
                channelConfigs.add(channelConfig);
                String tenant = briChannel.getTenant();
                FanscarConfig fanscarConfig = (FanscarConfig) briChannelService.getChannelConfig(tenant, ChannelEnum.FANSCAR.getName(), new FanscarConfig());
                Date begin = DateUtils.strToDateTime(param.getString("begin"));
                Date end = DateUtils.strToDateTime(param.getString("end"));
                try {
                    fanscarOrderService.getFanscarHotelOrders(fanscarConfig, tenant, begin, end, briChannel.getId());
                } catch (Exception e) {
                    sendEmailToDev("泛嘉酒店", e);
                }
            }
        }
    }

    /**
     * 手动拉取泛嘉机票订单
     * @param param
     */
    @RequestMapping("/fanscar/getFanscarFlightOrders")
    public void getFanscarFlightOrders(@RequestBody JSONObject param) {
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.FANSCAR.getName());
            List<String> channelConfigs = new ArrayList<>();
            for (BriChannel briChannel : briChannels) {
                String channelConfig = briChannel.getConfig();
                if (channelConfigs.contains(channelConfig)) {
                    continue;
                }
                channelConfigs.add(channelConfig);
                String tenant = briChannel.getTenant();
                FanscarConfig fanscarConfig = (FanscarConfig) briChannelService.getChannelConfig(tenant, ChannelEnum.FANSCAR.getName(), new FanscarConfig());
                Date begin = DateUtils.strToDateTime(param.getString("begin"));
                Date end = DateUtils.strToDateTime(param.getString("end"));
                try {
                    fanscarOrderService.getFanscarFlightOrders(fanscarConfig, tenant, begin, end, briChannel.getId());
                } catch (Exception e) {
                    sendEmailToDev("泛嘉机票", e);
                }
            }
        }
    }

    /**
     * 手动拉取滴滴用车订单
     * @param param
     */
    @RequestMapping("/didi/getDiDiCarOrders")
    public void getDiDiCarOrders(@RequestBody JSONObject param) {
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.DIDI.getName());
            for (BriChannel briChannel : briChannels) {
                String tenant = briChannel.getTenant();
                DiDiConfig diDiConfig = (DiDiConfig) briChannelService.getChannelConfig(tenant, ChannelEnum.DIDI.getName(), new DiDiConfig());
                String fetchDate = param.getString("fetchDate");
                try {
                    diDiOrderService.getDiDiOrderList(diDiConfig, fetchDate, tenant, briChannel.getId());
                } catch (Exception e) {
                    sendEmailToDev("滴滴用车", e);
                }
            }
        }
    }

    /**
     * 手动拉取携程商旅通订单
     * @param param
     */
    @RequestMapping("/ctripSme/getCtripSmeOrders")
    public void getCtripSmeOrders(@RequestBody JSONObject param){
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.CTRIPSME.getName());
            if (briChannels != null && briChannels.size() > 0) {
                for (BriChannel briChannel : briChannels) {
                    String tenant = briChannel.getTenant();
                    String begin = param.getString("begin");
                    String end = param.getString("end");
                    try {
                        ctripSmeOrderService.getCtripSmeOrderList(tenant,begin,end);
                    } catch (Exception e) {
                        sendEmailToDev("携程商旅通", e);
                    }
                }
            }
        }
    }

    /**
     * 手动拉取携程大客户订单
     * @param param
     */
    @RequestMapping("/ctripBC/getCtripBCOrders")
    public void getCtripBcOrders(@RequestBody JSONObject param){
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.CTRIPBC.getName());
            if (briChannels != null && briChannels.size() > 0) {
                for (BriChannel briChannel : briChannels) {
                    CtripBCConfig config  = JSONObject.parseObject(briChannel.getConfig(), CtripBCConfig.class);
                    String orderGetMod = config.getOrderGetMod();
                    String tenant = briChannel.getTenant();
                    String orgpk = briChannel.getOrgpk();
                    String begin = param.getString("begin");
                    String end = param.getString("end");
                    if ("1".equals(orderGetMod)) {
                        ctripBCOrderService.getCtripBCFlightSettleOrders(tenant,orgpk,begin,end,briChannel.getId(), config);

                        ctripBCOrderService.getCtripBCHotelSettleOrders(tenant,orgpk,begin,end,briChannel.getId(), config);

                        ctripBCOrderService.getCtripBCTrainSettleOrders(tenant,orgpk,begin,end,briChannel.getId(), config);

                        ctripBCOrderService.getCtripBCCarSettleOrders(tenant,orgpk,begin,end,briChannel.getId(), config);
                    } else {
                        try {
                            ctripBCOrderService.getCtripBCOrders(tenant,orgpk,begin,end,briChannel.getId(), config);
                        } catch (Exception e) {
                            sendEmailToDev("携程大客户", e);
                        }
                    }
                }
            }
        }
    }

    /**
     * 手动拉取中兴机票订单
     * @param param
     */
    @RequestMapping("/zte/getFlightOrders")
    public void getZteFlightOrders(@RequestBody JSONObject param){
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.ZTEITC.getName());
            if (briChannels != null && briChannels.size() > 0) {
                for (int i = 0; i < briChannels.size(); i++) {
                    BriChannel briChannel = briChannels.get(i);
                    String tenant = briChannel.getTenant();
                    String orgpk = briChannel.getOrgpk();
                    int channelId = briChannel.getId();
                    //begin和end精确到时分秒
                    String begin = param.getString("begin");
                    String end = param.getString("end");
                    try {
                        zteitcOrderService.getFlightOrderList(tenant, orgpk, channelId, begin, end);
                    } catch (Exception e) {
                        sendEmailToDev("中兴机票", e);
                    }
                }
            }
        }
    }

    /**
     * 手动拉取中兴火车票订单
     * @param param
     */
    @RequestMapping("/zte/getTrainOrders")
    public void getZteTrainOrders(@RequestBody JSONObject param){
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.ZTEITC.getName());
            if (briChannels != null && briChannels.size() > 0) {
                for (int i = 0; i < briChannels.size(); i++) {
                    BriChannel briChannel = briChannels.get(i);
                    String tenant = briChannel.getTenant();
                    String orgpk = briChannel.getOrgpk();
                    int channelId = briChannel.getId();
                    //begin和end精确到时分秒
                    String begin = param.getString("begin");
                    String end = param.getString("end");
                    try {
                        zteitcOrderService.getTrainOrderList(tenant, orgpk, channelId, begin, end);
                    } catch (Exception e) {
                        sendEmailToDev("中兴火车票", e);
                    }
                }
            }
        }
    }

    /**
     * 手动拉取中兴酒店订单
     * @param param
     */
    @RequestMapping("/zte/getHotelOrders")
    public void getZteHotelOrders(@RequestBody JSONObject param){
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.ZTEITC.getName());
            if (briChannels != null && briChannels.size() > 0) {
                for (int i = 0; i < briChannels.size(); i++) {
                    BriChannel briChannel = briChannels.get(i);
                    String tenant = briChannel.getTenant();
                    String orgpk = briChannel.getOrgpk();
                    int channelId = briChannel.getId();
                    //begin和end精确到时分秒
                    String begin = param.getString("begin");
                    String end = param.getString("end");
                    try {
                        zteitcOrderService.getHotelOrderList(tenant,orgpk,channelId,begin,end);
                    } catch (Exception e) {
                        sendEmailToDev("中兴酒店", e);
                    }
                }
            }
        }
    }

    /**
     * 查看商旅服务的版本
     * @param param
     */
    @RequestMapping(value = "/version",method = RequestMethod.POST)
    public String getVersion(@RequestBody JSONObject param) {
        if (param != null && "135246".equals(param.getString("token"))) {
            return brigadeVersion;
        }
        return "参数有误";
    }

    /**
     * 手动拉取途牛订单
     * @param param
     */
    @RequestMapping(value = "/tuniu/getOrders",method = RequestMethod.POST)
    public void getTuniuOrders(@RequestBody JSONObject param) {
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.TUNIU.getName());
            if (briChannels != null && briChannels.size() > 0) {
                for (int i = 0; i < briChannels.size(); i++) {
                    BriChannel briChannel = briChannels.get(i);
                    String tenant = briChannel.getTenant();
                    String orgpk = briChannel.getOrgpk();
                    int channelId = briChannel.getId();
                    //设置页码和每页条数
                    int pagesize = 10;
                    int pagenumber = 1;
                    try {
                        TuniuOrderService.getTuniuOrderList(tenant, orgpk, param.getString("begin"),param.getString("end"), pagenumber, pagesize, channelId);
                    } catch (Exception e) {
                        sendEmailToDev("途牛",e);
                    }
                }
            }
        }
    }

    /**
     * 手动拉取途家订单
     * @param param
     */
    @RequestMapping(value = "/tujia/getOrders",method = RequestMethod.POST)
    public void getTujiaOrders(@RequestBody JSONObject param) {
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.TUJIA.getName());
            if (briChannels != null && briChannels.size() > 0) {
                for (int i = 0; i < briChannels.size(); i++) {
                    BriChannel briChannel = briChannels.get(i);
                    String tenant = briChannel.getTenant();
                    String orgpk = briChannel.getOrgpk();
                    String phone = briChannel.getPhone();
                    int channelId = briChannel.getId();
                    //设置页码和每页条数
                    int pagesize = 20;
                    int pagenumber = 0;
                    //注意：日期应精确到时分秒
                    try {
                        TujiaOrderService.getTujiaOrderList(tenant, orgpk, phone, param.getString("begin"),param.getString("end"), pagesize, pagenumber, channelId);
                    } catch (Exception e) {
                        sendEmailToDev("途家",e);
                    }
                }
            }
        }
    }

    /**
     * 手动拉取差旅壹号订单
     * @param param
     */
    @RequestMapping(value = "/clyh/getOrders",method = RequestMethod.POST)
    public void getClyhOrders(@RequestBody JSONObject param) {
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.CLYH.getName());
            CopyOnWriteArrayList<String> channelConfigs = new CopyOnWriteArrayList<>();
            if (briChannels != null && briChannels.size() > 0) {
                for (int i = 0; i < briChannels.size(); i++) {
                    BriChannel briChannel = briChannels.get(i);
                    String channelConfig = briChannel.getConfig();
                    if (channelConfigs.contains(channelConfig)) {
                        continue;
                    }
                    channelConfigs.add(channelConfig);
                    String tenant = briChannel.getTenant();
                    String orgpk = briChannel.getOrgpk();
                    try {
                        CLYHOrderService.getOrderList(tenant, orgpk, param.getString("begin"),param.getString("end"), briChannel);
                    } catch (Exception e) {
                        sendEmailToDev("差旅壹号",e);
                    }
                }
            }
        }
    }

    /**
     * 手动拉取中航易购用车订单
     * @param param
     */
    @RequestMapping(value = "/rtpnr/getCarOrder",method = RequestMethod.POST)
    public void getCarOrder(@RequestBody JSONObject param) {
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> briChannels=briChannelService.getEnableChannelByChannel(ChannelEnum.RTPNR.getName());
            String begin=param.getString("begin");
            String end= param.getString("end");
            if(briChannels!=null&&briChannels.size()>0) {
                for(int i=0;i<briChannels.size();i++) {
                    BriChannel briChannel=briChannels.get(i);
                    String tenant=briChannel.getTenant();
                    String orgpk=briChannel.getOrgpk();
                    try {
                        rtpnrOrderService.getCarOrderList(tenant,orgpk,briChannel.getId(),begin,end);
                    } catch (Exception e) {
                        sendEmailToDev("中航易购用车",e);
                    }
                }
            }
        }
    }

    /**
     * 手动拉取中航易购火车票订单
     * @param param
     */
    @RequestMapping(value = "/rtpnr/getTrainOrder",method = RequestMethod.POST)
    public void getTrainOrder(@RequestBody JSONObject param) {
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> briChannels=briChannelService.getEnableChannelByChannel(ChannelEnum.RTPNR.getName());
            String begin=param.getString("begin");
            String end= param.getString("end");
            if(briChannels!=null&&briChannels.size()>0) {
                for(int i=0;i<briChannels.size();i++) {
                    BriChannel briChannel=briChannels.get(i);
                    String tenant=briChannel.getTenant();
                    String orgpk=briChannel.getOrgpk();
                    try {
                        rtpnrOrderService.getTrainOrderList(tenant,orgpk,briChannel.getId(),begin,end);
                    } catch (Exception e) {
                        sendEmailToDev("中航易购火车票",e);
                    }
                }
            }
        }
    }

    /**
     * 手动拉取中航易购机票订单
     * @param param
     */
    @RequestMapping(value = "/rtpnr/getFlightOrder",method = RequestMethod.POST)
    public void getFlightOrder(@RequestBody JSONObject param) {
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> briChannels=briChannelService.getEnableChannelByChannel(ChannelEnum.RTPNR.getName());
            String begin=param.getString("begin");
            String end= param.getString("end");
            if(briChannels!=null&&briChannels.size()>0) {
                for(int i=0;i<briChannels.size();i++) {
                    BriChannel briChannel=briChannels.get(i);
                    String tenant=briChannel.getTenant();
                    String orgpk=briChannel.getOrgpk();
                    try {
                        rtpnrOrderService.getFlightOrderList(tenant,orgpk,briChannel.getId(),begin,end);
                    } catch (Exception e) {
                        sendEmailToDev("中航易购机票",e);
                    }
                }
            }
        }
    }

    /**
     * 手动拉取中航易购酒店订单
     * @param param
     */
    @RequestMapping(value = "/rtpnr/getHotelOrder",method = RequestMethod.POST)
    public void getHotelOrder(@RequestBody JSONObject param) {
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.RTPNR.getName());
            String begin=param.getString("begin");
            String end= param.getString("end");
            if (briChannels != null && briChannels.size() > 0) {
                for (int i = 0; i < briChannels.size(); i++) {
                    BriChannel briChannel = briChannels.get(i);
                    String tenant = briChannel.getTenant();
                    String orgpk = briChannel.getOrgpk();
                    try {
                        rtpnrOrderService.getHotelOrderList(tenant,orgpk,briChannel.getId(),begin,end);
                    } catch (Exception e) {
                        sendEmailToDev("中航易购酒店",e);
                    }
                }
            }
        }
    }

    /**
     * 手动拉取携程（大客户）订单
     * @param param
     */
    @RequestMapping(value = "/ctripBC/getOrders",method = RequestMethod.POST)
    public Msg getCtripBCOrders(@RequestBody JSONObject param) {
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.CTRIPBC.getName());
            List<String> channelConfigList = new ArrayList<>();
            String begin = param.getString("begin");
            String end = param.getString("end");
            // String tenant = param.getString("tenant");
            for (BriChannel briChannel : briChannels) {
                CtripBCConfig config  = JSONObject.parseObject(briChannel.getConfig(), CtripBCConfig.class);
                String orderGetMod = config.getOrderGetMod();
                if (channelConfigList.contains(config.getAppKey())){
                    continue;
                } else {
                    channelConfigList.add(config.getAppKey());
                }
                String tenant = briChannel.getTenant();
                String orgpk = briChannel.getOrgpk();
                if ("1".equals(orderGetMod)) {
                    ctripBCOrderService.getCtripBCFlightSettleOrders(tenant,orgpk,begin,end,briChannel.getId(), config);

                    ctripBCOrderService.getCtripBCHotelSettleOrders(tenant,orgpk,begin,end,briChannel.getId(), config);

                    ctripBCOrderService.getCtripBCTrainSettleOrders(tenant,orgpk,begin,end,briChannel.getId(), config);

//                    ctripBCOrderService.getCtripBCCarSettleOrders(tenant,orgpk,begin,end,briChannel.getId(), config);
                } else {
                    ctripBCOrderService.getCtripBCOrders(tenant,orgpk,begin,end,briChannel.getId(), config);
                }

            }
        }
        return ResultUtil.success();
    }
    @RequestMapping("/huazhu/getOrders")
    public Object getOders(@RequestBody JSONObject param){
        String tenant = param.getString("tenant");
        String beginDate = param.getString("beginDate");
        String endDate = param.getString("endDate");
        String orgPk= param.getString("org");
        BriChannel briChannel = briChannelService.getEnableInfoByTenantOrg(tenant, orgPk, "huazhu");
        if (briChannel == null) {
            throw new BusinessException("未开通华住酒店");
        }
        List<BriChannel> channelList = briChannelService.getEnableChannelByChannel("huazhu");
        for (BriChannel channel : channelList) {
            if (StringUtil.isNotEmpty(tenant) && !tenant.equals(channel.getTenant())) {
                continue;
            }
            hzOrderService.getOrders(channel.getTenant(), channel.getOrgpk(), beginDate, endDate, briChannel);

        }
        return null;
    }

    /**
     * 拉单过程中出现异常时，给相关开发人员发邮件
     * @param fromTypeName 订单来源
     * @param e 获得的异常
     */
    public void sendEmailToDev(String fromTypeName, Exception e){
        Wrapper<Developer> developerWrapper = new EntityWrapper<>();
        developerWrapper.where("type={0}", "KF");
        List<Developer> developers = developerService.selectList(developerWrapper);
        String developerEmails = "";
        for (int j = 0; j < developers.size(); j++) {
            developerEmails += developers.get(j).getEmail();
            if (j < developers.size() - 1) {
                developerEmails += ",";
            }
        }
        Email email = new Email();
        String[] emails = developerEmails.split(",");
        email.setEmail(emails);
        email.setSubject(curEnv + "环境" + fromTypeName + "拉单失败");
        email.setContent(e.getMessage());
        try {
            log.error(e.getMessage());
            mailService.send(email);
        } catch (Exception e1) {
            log.error("提示" + curEnv + "环境" + fromTypeName + "订单拉单失败的邮件发送失败！" + e1.getMessage());
        }
    }

    /**
     * 滴滴用户同步
     * @param param
     */
    @RequestMapping(value = "/didi/syncUser",method = RequestMethod.POST)
    public void synDidiUsers(@RequestBody JSONObject param) {
        if (param != null && "135246".equals(param.getString("token"))) {
            List<BriChannel> channelList = briChannelService.getEnableChannelByChannel(ChannelEnum.DIDI.getName());
            for (BriChannel channel : channelList) {
                try {
//                    if (BriChannel.NEEDSYNCUSER == channel.getNeedSyncUser()) {
                        String tenant = channel.getTenant();
                        StringBuilder sb = new StringBuilder("滴滴用户同步，租户：").append(tenant).append("\r\n");
                        log.info("滴滴人员更新任务开始，租户{}", tenant);

                        List<UcUserDto> userList = userCenterService.getUcUsersByTenantId(tenant);
                        if (CollectionUtils.isEmpty(userList)) {
                            continue;
                        }
                        Wrapper<DiDiUser> wrapper = new EntityWrapper<>();
                        wrapper.eq(DiDiUser.TENANT, tenant);
                        List<DiDiUser> didiUserList = didiUserService.selectList(wrapper);

                        Map<String, UcUserDto> ucUserDtoMap = new HashMap<>();
                        List<DiDiUser> invalidUserList = new ArrayList<>();

                        userList.forEach(ucUserDto -> ucUserDtoMap.put(ucUserDto.getPhone(), ucUserDto));
                        //循环商旅服务本地已经缓存的用户
                        didiUserList.forEach(didiUser -> {
                            UcUserDto ucUser = ucUserDtoMap.get(didiUser.getPhone());
                            if (ucUser == null || ucUser.getDr() == 1 || ucUser.getValid() == 0) {
                                invalidUserList.add(didiUser);
                            }
                            ucUserDtoMap.remove(didiUser.getPhone());
                        });
                        //循环其他用户
                        sb.append("新增用户：").append("\r\n");
                        for (String phone : ucUserDtoMap.keySet()) {
                            UcUserDto ucUserDto = ucUserDtoMap.get(phone);
                            if (ucUserDto.getValid() == 0 || ucUserDto.getDr() == 1) {
                                invalidUserList.add(DiDiUtil.convertToDiDiUser(tenant, ucUserDto));
                            } else {
                                DiDiUser didiUser = DiDiUtil.convertToDiDiUser(tenant, ucUserDto);
                                try {
                                    didiUserService.addSingleUser(tenant, didiUser);
                                } catch (Exception e) {
                                    sb.append(phone).append(e.getMessage()).append("\r\n");
                                }
                            }
                        }
                        log.info("滴滴人员更新任务，删除用户，租户{}", tenant);
                        try {
                            didiUserService.deleteUsers(tenant, invalidUserList);
                        } catch (Exception e) {
                            sb.append("删除用户：").append("\r\n");
                            sb.append(e.getMessage()).append("\r\n");
                        }
                        MailSenderUtil.sendCommonMail(developerService.listEmailByType("YY"), "滴滴用户定时更新", sb.toString());
                        log.info("滴滴人员更新任务结束，租户{}", tenant);
//                    } else {
//                        log.info("该租户未配置同步用户");
//                    }
                } catch (Exception e) {
                    String msg = "滴滴更新异常，租户：" + channel.getTenant() + "异常信息：" + e.getMessage();
                    MailSenderUtil.sendCommonMail(developerService.listEmailByType("KF"), "滴滴人员更新异常结束", msg);
                }
            }
        }
    }




    /**
     * 手动拉取北方假日机票酒店订单
     * @param
     * @return
     */
    @RequestMapping("/bfjr/flightAndHotelOrder")
    public Msg getBfjrFlightHotelOrder(@RequestBody Map param){
        List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.BFJR.getName());
        List<String> channelConfigList = new ArrayList<>();
        for (BriChannel briChannel : briChannels) {
            String config = briChannel.getConfig();
            String tenant = briChannel.getTenant();
            Integer channelId = briChannel.getId();
            JSONObject configObject = JSONObject.parseObject(config);
            String appId = configObject.getString("appId");
            if (channelConfigList.contains(appId)){
                continue;
            } else {
                channelConfigList.add(appId);
            }
            bfjrOrderService.getBrjrOrderList(appId,param,tenant,channelId);

        }
        return ResultUtil.success();
    }
























}
